package com.android.support.jhf.utils;

/**
 * 坐标转换
 * 
 * @author jiahongfei
 * 
 */
public class CoordinateConversion {
	
	// Longitude 经度 例子 116
	// latitude  纬度 例子 39

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
		//谷歌转换百度
		//39.90742,116.374269谷歌坐标西单地铁站
		double[] gg2bd = gg2bd(39.90742,116.374269);
		System.out.println("gg2bd : " + gg2bd[0] + "," + gg2bd[1]);
		
		//百度转换谷歌
		//116.380226,39.922136百度坐标灵境胡同地铁站
		double[] bd2gg = bd2gg(39.922136,116.380226);
		System.out.println("bd2gg : " + bd2gg[1] + "," + bd2gg[0]);
		
	}

	public static final double X_IP = 3.14159265358979324 * 3000.0 / 180.0;

	public static double[] gg2bd(double gg_lat, double gg_lon) {
		double x = gg_lon, y = gg_lat;
		double z = Math.sqrt(x * x + y * y) + 0.00002 * Math.sin(y * X_IP);
		double theta = Math.atan2(y, x) + 0.000003 * Math.cos(x * X_IP);
		double bd_lon = z * Math.cos(theta) + 0.0065;
		double bd_lat = z * Math.sin(theta) + 0.006;
		return new double[] { bd_lon, bd_lat };
	}

	public static double[] bd2gg(double bd_lat, double bd_lon) {
		double x = bd_lon - 0.0065, y = bd_lat - 0.006;
		double z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_IP);
		double theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_IP);
		double gg_lon = z * Math.cos(theta);
		double gg_lat = z * Math.sin(theta);
		return new double[] { gg_lon, gg_lat };
	}

}
